#!/bin/bash

function copy_shells()
{
   set -x
   echo copy phoenix_ios.sh to 192.168.8.210 .......
   scp  /usr/local/scripts/build_scripts/phoenix_ios.sh  jenkins@192.168.8.210:/Users/jenkins/lebo/
   echo copy all shells to 192.168.8.233 .....
   scp -r    /usr/local/scripts/build_scripts/*  root@192.168.8.233:/usr/local/scripts/build_scripts/
   echo copy all shells to 192.168.8.204 .....
   scp -r  /usr/local/scripts/build_scripts/*  root@192.168.8.204:/usr/local/scripts/build_scripts/
   echo copy rec_app_info.conf send_app_info.conf to lebo cloud 120.76.42.131  .....
   scp -r  /usr/local/scripts/build_scripts/rec_app_info.conf  root@120.76.42.131:/root/helen/scripts/
   scp -r  /usr/local/scripts/build_scripts/send_app_info.conf  root@120.76.42.131:/root/helen/scripts/
   scp -r  /usr/local/scripts/build_scripts/sign_apk_tool/*  root@120.76.42.131:/root/helen/scripts/
   set +x
}

function check_inline()
{
    repo_url="ssh://gerrit@192.168.8.250:29418/phoenix"
    [ "$1" == "phoenix_send_sdk" ] && git clone "$repo_url"/standard/sdk/"source"  && cd source
    [ "$1" == "phoenix_sdk" ] && git clone "$repo_url"/SDK && cd SDK
    [ "$1" == "phoenix_Rec_App" ] && git clone "$repo_url"/standard/AndroidReceiver && cd AndroidReceiver
    [ "$1" == "phoenix_Send_App" ] && git clone "$repo_url"/standard/AndroidSender && cd AndroidSender
    [ "$1" == "phoenix_ios_app" ] && git clone "$repo_url"/AppleSenderApp && cd AppleSenderApp
    [ "$1" == "phoenix_ios_sdk" ] && git clone "$repo_url"/AppleSenderSDK && cd AppleSenderSDK

    git branch -a |grep remote  |grep -v HEAD |awk -F / '{print $3 }' > test3
    [ "$1" == "phoenix_Rec_App" ] && sed -i "/damai/d" test3
    [ "$1" == "phoenix_sdk" ] && sed -i "/r6.0.0_pptv_1.0/d" test3 && sed -i "/ding/d" test3
    [ "$1" == "phoenix_send_sdk" ] && sed -i "/phicomm/d" test3
    echo "tags:" >> test3
    git tag >> test3

    echo "======$1 branchs and tags ====="
    cat test3
}

function copy_apk()
{
    apk_ftp="ftp://192.168.8.251/output/Sign_apk/app_for_anl/"
    cdn_server="http://download.mirrorcast.cn/release"
    cdn_path="lebo@47.106.144.141:/lebosource/apk/release"
    #cdn_psw="source!@#321" #CIBN密码
    cdn_psw="Pass@123(*)" #CIBN密码

    curl -u aolingli:ALL1234!  $apk_ftp > apk_info
    cat apk_info |awk -F " " '{print $4}' > apk_info.tmp
    mv apk_info.tmp apk_info
    now_time=`date "+%Y%m%d%H%M"`
    [ "$1" == "1" ] && dst_dir="phoenix_send_app"
    [ "$1" == "2" ] && dst_dir="phoenix_app"
    for download_file in `cat apk_info`
        do
            echo download $download_file
            curl -u aolingli:ALL1234! -o "$download_file"  "$apk_ftp"$download_file
            mv $download_file "$now_time"_"$download_file"
            sshpass -p $cdn_psw  scp  "$now_time"_"$download_file" $cdn_path/$dst_dir/
            echo apk_md5sum as follow:
            md5sum "$now_time"_"$download_file"
            echo cdn_path=$cdn_server/$dst_dir/"$now_time"_"$download_file"
        done
 }

copy_scripts=$1
get_repo=$2
apk_2cdn=$3
echo ******************var:copy_scripts=$copy_scripts,get_branchs=$get_branchs,apk_2cdn=$apk_2cdn**************
[ "$copy_scripts" == "1" ] && copy_shells
[ "$get_repo" != "0" ] &&  check_inline $get_repo
[ "$apk_2cdn" != "0" ] && copy_apk $apk_2cdn
echo Done
